<?php

class User_UsersController extends Zend_Controller_Action
{
         protected $_session;
         protected $_user;
         protected $reglogin;
       

    public function init()
    {  
        $this->_user = new User_Model_User();
        $this->_session = new User_Model_Session();
        $this->reglogin = Zend_Registry::get('login');
        $this->regSession = Zend_Registry::get('session');
    }
 
    public function indexAction()
    {
        // action body      
      
    }

    public function loginAction()
    {   
//        echo "<br>users controllers : ";
//        echo "<br>modulo:";var_dump($this->_request->getModuleName());
//        echo "<br>controlador: ";var_dump($this->_request->getControllerName());
//        echo "<br><br><br>accion: "; var_dump($this->_request->getActionName());
//       die();

    	$this->view->title = 'zf Login - GNS ';
        $request = $this->getRequest();
        
        $form = new User_Form_LoginForm();
       
        if($request->isPost()){
            if($form->isValid($this->_request->getPost())){

                $fuser = $form->getValues();
                $iduser=$fuser['username'];
               
                $state_current = $this->_user->getStateUser($iduser);
      
                //verificando estado usuario::
                if(($state_current == "ACTIVE") ||  ($state_current == "FLOGIN") || ($state_current == "RESET") ){
			    $state_active = true;
                            $loginValido=  $this->_user->autenticar($form->getValues());
		}else{
                            $state_active = false;
                            $loginValido='';
                }

                if(is_array($loginValido)){

                    $date_start_session = new DateTime(date('Y-m-d H:i:s'));
                    $date_end_session = new DateTime(date('Y-m-d H:i:s'));                    

                    $datos['id_session'] = "";
                    $datos['id_user'] = $this->reglogin->rg_usuario["id_user"]; //variable de session 
                    $datos['date_start_session'] =$date_start_session->format('Y-m-d H:i:s');
                    $datos["date_end_session"] = $date_end_session->format("Y-m-d H:i:s") ;
                    $datos['state'] = 1 ;
                    //inserto en la tabla sys_session
                    $this->_session->insert($datos);
                    //selecciono la session generada para el usuario:
                    $session_valida=  $this->_session->validateSessionByUser($this->reglogin->rg_usuario["id_user"]);
                    //almaceno en zend registry la session
                    $this->reglogin->rg_session = $session_valida;

                    //echo "xxx>>>>".$loginValido['state'];die();
                    if ($loginValido['state']== 1)
                    {   
                        $this->_redirect('qpcr/index/upload');
                        //$this->_redirect('qpcr/upload/index');
                    }else{
                        $this->_redirect('/users/login');
                    }
                }


//                $loginValido=  $this->_user->autenticar($form->getValues());
//                if($loginValido){
//                $session['idsession'] = '';
//                $session['iduser'] = 2;
//                $zfsession= $this->_session->insert($session);
//                $regSession = Zend_Registry::get('session');
//                $regSession->session_usuario = $zfsession;
//
//                 $this->_redirect('qpcr/index/upload');
//
//                }else{
//                 //$this->_helper->FlashMessenger('Usuario o contraseña invalido');
//                 $this->_redirect('/users/login');
//                }

            }
      }


        $this->view->form = $form;
        
        
    }

    public function logoutAction()
    {
//        Zend_Auth::getInstance()->clearIdentity();
//        $this->_redirect('index/index');
       
        $rLogin = Zend_Registry::get('login');
        unset($rLogin->rg_usuario);
        $rSession = Zend_Registry::get('session');
        unset($rSession->rg_session);
        $this->_redirect('index/index');
    }

    
//    private function getAuthAdapter() {
//        $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
//        $authAdapter->setTableName('sis_user')
//                    ->setIdentityColumn('user_name')
//                    ->setCredentialColumn('password');
//
//        return $authAdapter;
//    }
  

}





